package com.yihu.wlyy.service.app.consult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;
import org.springside.modules.utils.Clock;

import com.yihu.wlyy.entity.doctor.Doctor;
import com.yihu.wlyy.entity.doctor.DoctorComment;
import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.repository.ConsultMoViDao;
import com.yihu.wlyy.repository.ConsultTeamDao;
import com.yihu.wlyy.repository.ConsultTeamDoctorDao;
import com.yihu.wlyy.repository.DoctorCommentDao;
import com.yihu.wlyy.repository.DoctorDao;
import com.yihu.wlyy.repository.PatientDao;
import com.yihu.wlyy.service.BaseService;

@Component
@Transactional(rollbackFor = Exception.class)
public class DoctorCommentService extends BaseService {

	private Clock clock = Clock.DEFAULT;

	@Autowired
	private ConsultTeamDoctorDao consultTeamDoctorDao;
	@Autowired
	private DoctorCommentDao doctorCommentDao;
	@Autowired
	private ConsultTeamDao consultDao;
	@Autowired
	private ConsultMoViDao consultMoViDao;
	@Autowired
	private PatientDao patientDao;
	@Autowired
	private DoctorDao doctorDao;

	public void setClock(Clock clock) {
		this.clock = clock;
	}

	/**
	 * 三师咨询患者评价
	 * @param patient 患者标识
	 * @param consultMoVi 视频咨询标识
	 * @param content 评价内容
	 * @param star 星级
	 * @param type 评价类型：1视频咨询评价、2三师咨询评价
	 * @return
	 */
	public JSONArray consultComment(String patient, String consult, String content, int star, int type) {
		// 查询患者信息
		Patient pt = patientDao.findByCode(patient);
		List<DoctorComment> list = new ArrayList<DoctorComment>();
		// 查询有参与过回复的医生
		String[] doctors = consultTeamDoctorDao.findReplyDoctor(consult);
		if (doctors != null && doctors.length > 0) {
			JSONArray array = new JSONArray();
			for (String doctor : doctors) {
				if (StringUtils.isEmpty(doctor)) {
					continue;
				}
				// 查询医生信息
				Doctor d = doctorDao.findByCode(doctor);
				DoctorComment comment = new DoctorComment();
				comment.setContent(content);
				comment.setDel("1");
				comment.setDoctor(d.getCode());
				comment.setDname(d.getName());
				comment.setStar(star);
				comment.setUname(pt.getName());
				comment.setUser(pt.getCode());
				comment.setCode(getCode());
				comment.setType(type);
				comment.setCzrq(clock.getCurrentDate());
				comment.setDel("1");
				list.add(comment);

				JSONObject json = new JSONObject();
				json.put("doctor", doctor);
				// 咨询统计
				json.put("flag", 1);
				if (type == 1) {
					// 视频咨询
					json.put("type", 2);
				} else if (type == 2) {
					// 三师咨询
					json.put("type", 1);
				}
				json.put("star", star);
				array.put(json);
			}
			// 保存
			doctorCommentDao.save(list);
			// 更新患者评价状态为已评价
			consultDao.updateComment(content, star, consult);
			return array;
		}
		return null;
	}

	/**
	 * 视频咨询患者评价
	 * @param patient 患者标识
	 * @param consultMoVi 视频咨询标识
	 * @param content 评价内容
	 * @param doctor 医生标识
	 * @param star 星级
	 * @param type 评价类型：1视频咨询评价、2三师咨询评价
	 * @return
	 */
	public DoctorComment consultMoViComment(String patient, String consultMoVi, String content, String doctor, int star, int type) {
		// 查询医生信息
		Doctor d = doctorDao.findByCode(doctor);
		Patient pt = patientDao.findByCode(patient);
		DoctorComment comment = new DoctorComment();
		comment.setContent(content);
		comment.setDel("1");
		comment.setDoctor(d.getCode());
		comment.setDname(d.getName());
		comment.setStar(star);
		comment.setUname(pt.getName());
		comment.setUser(pt.getCode());
		comment.setCode(getCode());
		comment.setCzrq(clock.getCurrentDate());
		comment.setDel("1");
		// 保存
		comment = doctorCommentDao.save(comment);
		if (comment != null) {
			// 更新评价状态
			consultMoViDao.updateComment(comment, consultMoVi);
		}
		return comment;
	}

	/**
	 * 查询指定医生的评价记录
	 * @return
	 */
	public Page<DoctorComment> findByDoctor(String doctor, int id, int pagesize) {
		if (id < 0) {
			id = 0;
		}
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);
		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		// 未指定医生
		filters.put("doctor", new SearchFilter("doctor", Operator.EQ, doctor));
		if (id > 0) {
			// 比当前ID小
			filters.put("id", new SearchFilter("id", Operator.LT, id));
		}
		// 未作废
		filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
		Specification<DoctorComment> spec = DynamicSpecifications.bySearchFilter(filters.values(), DoctorComment.class);
		return doctorCommentDao.findAll(spec, pageRequest);
	}
}
